import os
import sys
import socket

host = 'localhost' # Refer's to "This Computer"

def sendToMaya(message):
    home = os.environ['MAYA_SCRIPT_PATH_BASE']
    with open(home + "\\MELVIM\\port","r") as f:
            port = int(f.read().strip());

    maya = None
    if port:
        try:
            # Connect to Maya Command Port
            maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            maya.connect( (host,port) )

            # Send Command Through Socket --> Can Only Send MEL Commands
            maya.send(message.encode("utf-8"))
            print("sent message '%s'" % message)

        finally:
            # Close Socket Connection
            if maya:
                maya.close()
                print("closed connection")


if __name__ == '__main__':
    message = " ".join(sys.argv[1:])
    sendToMaya(message)
